package com.zzptc.easyshop.db;

import com.zzptc.easyshop.util.MyUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;

public class MysqlDB implements IDB {
    //全局变量
    private Connection conn;
    private Map map;
//    private String DRIVER="com.mysql.jdbc.Driver";
//    private String URL="jdbc:mysql://localhost:3306/easybuy?useUnicode=true&characterEncoding=utf8";
//    private String UNAME="root";
//    private String PASS="root";
    private String DRIVER;
    private String URL;
    private String UNAME;
    private String PASS;
    @Override
    public Connection getConn() throws SQLException {
        try {
            map=MyUtil.readXML("dao-config.xml");//用自己写的工具类读取配置文件信息
            DRIVER= (String) map.get("driver");
            URL= (String) map.get("url");
            UNAME=(String) map.get("uname");
            PASS=(String) map.get("pwd");
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Class.forName(DRIVER);
            conn = DriverManager.getConnection(URL,UNAME,PASS);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return conn;
    }

    @Override
    public void closeConn() throws SQLException {
        conn.close();

    }
}
